/*
 * @[H]:  Copyright (c) 2021 Phytium Information Technology, Inc. 
 * 
 *  SPDX-License-Identifier: Apache-2.0. 
 * 
 * @Date: 2021-08-31 09:10:07
 * @LastEditTime: 2021-09-01 16:21:04
 * @Description:  Description of file
 * @Modify History: 
 * * * Ver   Who        Date         Changes
 * * ----- ------     --------    --------------------------------------
 */

#ifndef COMMON_H
#define COMMON_H


#include <metal/atomic.h>
#include <metal/alloc.h>
#include <metal/irq.h>
#include <metal/errno.h>
#include <metal/sys.h>
#include <metal/cpu.h>
#include <metal/io.h>
#include <metal/device.h>


/* Devices names */
#define BUS_NAME        "generic"
#define SHM_DEV_NAME    "3ed80000.shm"


// #define IPI_MASK        1 /* IPI mask for kick from APU.  We use PL0 IPI in this demo. */


static inline void wait_for_interrupt()
{
	metal_asm volatile("wfi");
}


/**
 * @breif wait_for_notified() - Loop until notified bit
 *        in channel is set.
 *
 * @param[in] notified - pointer to the notified variable
 */
static inline void  wait_for_notified(atomic_int *notified)
{
	unsigned int flags;

	do {
		flags = metal_irq_save_disable();
		if (!atomic_flag_test_and_set(notified)) {
			metal_irq_restore_enable(flags);
			break;
		}
		wait_for_interrupt();
		metal_irq_restore_enable(flags);
	} while(1);
}


#endif // !